const MyPromise = require('./MyPromise');

// 示例1，执行器
const promsie1 = new MyPromise((resolve, reject) => {
  console.log('p1构造器执行成功');
  // resolve('此时执行了resolve方法');
  reject('此时执行了reject方法');
});

promsie1.then(console.log, console.log);

// 示例2 执行器内异步
const promise2 = new MyPromise((resolve, reject) => {
  setTimeout(() => {
    resolve('异步成功');
    // resolve('异步失败');
  }, 0);
});

promise2.then(console.log, console.log);


// 示例 3
MyPromise.resolve(100).then(console.log)


const promise4 = new MyPromise((resolve, reject) => {
  resolve('finally成功');
})

// 示例4
promise4.
  finally(() => {
    console.log('finally');
    // return 'finall-100';
  })
  .then((value) => {
    console.log('finally后then', value);
    throw TypeError('类型错误')
  })

const promise5 = new MyPromise((resolve, reject) => {
  resolve('错误检测-初始成功');
})

promise5.then(() => {
  throw new Error('错误了，有检测吗')
}, () => {

}).catch(reason => {
  console.log('错误是', reason)
})


const p1 = new MyPromise((resolve, reject) => {
  console.log('1');
  resolve(1);
});

const p3 = new MyPromise((resolve, reject) => {
  setTimeout(() => {
    resolve(3);
  }, 100);
});

MyPromise.all([
  p1,
  2,
  p3
]).then(([
  p1Res,
  p2Res,
  p3Res
]) => {
  console.log('p1Res:', p1Res, '--', 'p2Res:', p2Res, '---', 'p3Res', p3Res);
})